package dao.resource;

import java.util.List;

import javax.ejb.Stateless;
import javax.inject.Named;

import entity.resource.*;
import entity.task.Task;
import entity.tasktype.TaskType;
import entity.user.User;
import dao.core.*;

@Named
@Stateless
public class ResourceDAO extends GenericHibernateDAO<Resource, Long> {

	public ResourceDAO() {
		super(Resource.class);
	}

	@SuppressWarnings("unchecked")
	public List<Resource> findByTaskId(Long taskId) {

		return getSession()
				.createQuery(new StringBuilder()
								.append("from Task t ")
								.append("where t.resource.id = :id")
								.toString())
				.setParameter("id", taskId)				
				.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<Resource> findFreeResources() {

		return getSession()
				.createQuery(new StringBuilder()
								.append("from Resource r ")
								.append("where r.task.id Is Null")
								.toString())			
				.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<Resource> findByTask(Task task) {

		return getSession()
				.createQuery(new StringBuilder()
								.append("from Task t")
								.append(" where t.resource.id = :id")
								.toString())
				.setParameter("id", task.getId() )
				.list();

	}
}